﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SquidConsult.BLL;
using System.Data.SqlClient;
using SquidConsult.DAL;
using System.Data;
using System.IO;


namespace SquidConsult.UI.Consultant
{
    public partial class ViewReport : System.Web.UI.Page
    {
        private ConsultantBL consultant = new ConsultantBL();
        System.Guid consultantID;

        #region Load Reports
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipUser userID = Membership.GetUser();
            string conId = userID.ProviderUserKey.ToString();
            consultantID = System.Guid.Parse(conId);

            LoadReport();
        }

        private void LoadReport()
        {
            try
            {
                if (ReportDropDownList.SelectedIndex == 1)
                    ReportGridView.DataSource = consultant.GetStatusReport(consultantID, "Approved");
                else if (ReportDropDownList.SelectedIndex == 2)
                    ReportGridView.DataSource = consultant.GetStatusReport(consultantID, "OnGoing");
                else
                    ReportGridView.DataSource = consultant.GetAllReport(consultantID);

                ReportGridView.DataBind();
            }
            catch (Exception ex)
            {
                LoadError.Text = "Reports load fail : The following error occured: " + ex.Message;
            }   
        }

        protected void ReportDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadReport();
        }
        #endregion 

        #region Load Expense 
        protected void ReportGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ExpenseGridView.DataSource = consultant.GetExpense(Guid.Parse(ReportGridView.SelectedRow.Cells[1].Text));
                ExpenseGridView.DataBind();
            }
            catch (Exception ex)
            {
                LoadError.Text = "Reports load fail : The following error occured: " + ex.Message;
            }
        }

        protected void ReportGridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].CssClass = "hiddencol";
        }
        #endregion 

        #region Download Repecipt
        protected void DownloadButton_Click(object sender, EventArgs e)
        {
            string receiptPath = consultant.GetReceiptPath(Guid.Parse(ExpenseGridView.SelectedRow.Cells[1].Text));
            FileStream fileStream = new FileStream(receiptPath, FileMode.Open, FileAccess.Read);
            Byte[] binaryData = new Byte[fileStream.Length];
            fileStream.Read(binaryData, 0, (Int32)fileStream.Length);
            fileStream.Close();
            Response.ContentType = "pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=Receipt.pdf");
            Response.AddHeader("Content-length", binaryData.Length.ToString());
            Response.BinaryWrite(binaryData);
            Response.Flush();
            Response.Clear();
        }

        protected void ExpenseGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            DownloadPanel.Visible = true;
        }
        #endregion
    }
}